package com.example.administrator.testxingpiao.httputils;

import com.example.administrator.testxingpiao.config.ConfigUrl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2016/8/4.
 */
public class RetrofitHelper {

    private static RetrofitHelper instance;
    private Gson gson;
    private Retrofit retrofit;
    //单例
    public static synchronized RetrofitHelper getInstance(){

        if(instance == null){

            instance = new RetrofitHelper();
        }
        return instance;
    }

    public <T> T createRetrofitService(Class<T> service){

        if(retrofit == null){
            createGson();
            initRetrofit();
        }
        return retrofit.create(service);
    }



    private void initRetrofit(){
        retrofit = new Retrofit.Builder()
                .baseUrl(ConfigUrl.Path.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }

    private void createGson() {
        gson = new GsonBuilder()
                .serializeNulls()
                .create();
    }
}
